import JQuery from 'jquery';
import message from '@/js/util/message.js';

export function http(url, param = {}, method = 'POST', type = 'json', callback = null) {
    let ajax = {
        url: url,
        type: method,
        data: param
    };
    if (callback == null) {
        ajax.async = false;
        let data = JQuery.ajax(ajax);
        if (type == 'json') {
            return data.responseJSON;
        } else {
            return data.responseText;
        }
    } else {
        ajax.async = true;
        ajax.success = function(responseText) {
            if (callback.success) {
                callback.success(responseText);
            }
        };
        ajax.error = function(responseText) {
            if (callback.error) {
                callback.error(responseText);
            }
        };
        JQuery.ajax(ajax);
    }
};

export function socket(url, command) {
    let param = new Promise(function(resolve, reject) {
        if ("WebSocket" in window) {
            if (!url) {
                message.message("请输入正确的服务器地址", 'warning');
                reject();
            } else {
                let websocket = new WebSocket(url);
                let index = 0;
                let list = [];
                let timer = window.setTimeout(function() {
                    message.message("WebSocket连接超时错误", 'error');
                    websocket.close();
                }, 30000);
                websocket.onmessage = function(e) {
                    let ws = e.currentTarget;
                    let data = JSON.parse(e.data);
                    if (data.command == command[index].command) {
                        list[index] = e;
                        index++;
                        if (index < command.length) {
                            ws.send(JSON.stringify(command[index]));
                        } else {
                            ws.close();
                            resolve(list);
                        }
                        window.clearTimeout(timer);
                    }
                };
                websocket.onclose = function(e) {
                    //console.log("连接关闭");
                };
                websocket.onerror = function(e) {
                    message.message("WebSocket连接发生错误", 'error');
                };
                websocket.onopen = function(e) {
                    let ws = e.currentTarget;
                    ws.send(JSON.stringify(command[index]));
                    //console.log("连接成功", command);
                };
            }
        } else {
            message.message("该浏览器不支持websocket!", 'warning');
            reject();
        }
    });
    return param;
};

export function httpFormData(url, param = {}, method = 'POST', type = 'json', callback = null) {
    let ajax = {
        url: url,
        type: method,
        processData: false,
        contentType: false,
        data: param
    };
    if (callback == null) {
        ajax.async = false;
        let data = JQuery.ajax(ajax);
        if (type == 'json') {
            return data.responseJSON;
        } else {
            return data.responseText;
        }
    } else {
        ajax.async = true;
        ajax.success = function(responseText) {
            if (callback.success) {
                callback.success(responseText);
            }
        };
        ajax.error = function(responseText) {
            if (callback.error) {
                callback.error(responseText);
            }
        };
        JQuery.ajax(ajax);
    }
};